var wso = {
	WEBsocket: null,
	onMessageHandler: null,
	wsUrl: null,
	onFailedHandler: null,

	init: function(url, onMessage, onFailed) {
		this.wsUrl = url;
		this.onMessageHandler = onMessage;
		this.onFailedHandler = onFailed;
		if (checkWS()) {
			console.log("已有连接不用create-----------", WEBsocket);
			return;
		}
		console.log("create ws ", currentType);
		this.WEBsocket = new WebSocket(url);
		this.WEBsocket.opendid = (new Date).getTime();
		/*  WEBsocket.onopen = function () {
			heartCheck.start();
		  };
		  WEBsocket.onerror = function (e) {
			console.log("连接失败web-----------", e, lockReconnect);
			WEBsocketOnerror && WEBsocketOnerror(e);
			//reconnect(url);
		  };
		  WEBsocket.onclose = function (e) {
			console.log(
			  "关闭了-----------",
			  e.code + " code " + e.reason + " reason " + e.wasClean
			);
			WEBsocketOnclose && WEBsocketOnclose(e);
			reconnect(url); // 重连
		  };*/
		WEBsocket.onmessage = function(event) {
			//console.log("连接成功web-----------", event);
			if (event.data === "[_heart_beat_]") {
				//心跳报文应答直接忽略
				return;
			}
			onMessageHandler && onMessageHandler(event);
			//heartCheck.reset().start();
		};
	},

	/*let reconnect = (url) => {
	  if(checkWS()){
		return;
	  }
	  if (lockReconnect) return; //没连接上会一直重连，设置延迟避免请求过多
	  lockReconnect = true; //event 为服务端传输的消息，在这里可以处理
	  console.log("正在链接-----------");
	  setTimeout(function () {
		// console.log(WEBsocket);
		createWebSocket(url, WEBsocket.todo,WEBsocketOnclose,WEBsocketOnerror);
		lockReconnect = false;
	  }, 5000);
	};
	let heartCheck = {
	  timeout: 30000, //30秒
	  timeoutObj: null,
	  reset: function () {
		clearInterval(this.timeoutObj);
		return this;
	  },
	  start: function () {
		this.timeoutObj = setInterval(function () {
		  //这里发送一个心跳，后端收到后，返回一个心跳消息，
		  //onmessage拿到返回的心跳就说明连接正常
		  let data = {
			msg: "[_lxzw_heart_beat_]",
		  };
		  if(checkWS()){
			WEBsocket.send(JSON.stringify(data));
		  }
		}, this.timeout);
	  },
	};*/

	checkWS: function() {
		console.log("check ws-----------", currentType);
		console.log("check ws-----------", WEBsocket && WEBsocket.readyState === WEBsocket.OPEN)
		return this.WEBsocket && this.WEBsocket.readyState === WEBsocket.OPEN;
	},

	//关闭连接
	closeWebSocket: function() {
		console.log("close ws ", currentType);
		this.WEBsocket && this.WEBsocket.close();
		//heartCheck.reset();
	},
	sendWsMsg: function(msg) {
		if (!checkWS()) {
			createWebSocket(this.wsUrl, this.onMessageHandler, this.onFailedHandler);
			setTimeout(function() {
				if (!checkWS()) {
					this.onFailedHandler && this.onFailedHandler()
				} else {
					this.WEBsocket.send(msg);
				}
			}, 2000);
		} else {
			this.WEBsocket.send(msg);
		}

	}

}
